package com.longshihan.news.ui.gank.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.longshihan.news.R;
import com.longshihan.news.model.bean.GankItemBean;
import com.longshihan.news.util.ZDate;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @创建者 longshihan
 * @创建时间 2016/9/26 10:06
 */

public class TechAdapter extends RecyclerView.Adapter<TechAdapter.ViewHolder> {


    private LayoutInflater     mInflater;
    private List<GankItemBean> mList;
    private String             mTech;

    public TechAdapter(Context context, List<GankItemBean> list, String tech) {
        mInflater = LayoutInflater.from(context);
        this.mList = list;
        this.mTech = tech;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_tech, parent, false));
    }

    //    private Drawable mDrawable_android,mDrawable_ios,mDrawable_web;
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        //        if(mTech.equals(TechPresenter.TECH_ANDROID)) {
        //            holder.mTvTechTitle.setCompoundDrawables(R.drawable.item_ic_android,null,null,null);
        //        } else if(mTech.equals(TechPresenter.TECH_IOS)) {
        //            holder.mIconItemTech.setImageResource(R.mipmap.item_ic_apple);
        //        } else if(mTech.equals(TechPresenter.TECH_WEB)) {
        //            holder.mIconItemTech.setImageResource(R.mipmap.item_ic_web);
        //        }
        holder.mTvTechTitle.setText(mList.get(position).getDesc());
        if (!TextUtils.isEmpty(mList.get(position).getWho())) {
            holder.mTvTechAuthor.setText(mList.get(position).getWho());
        }
        String date = mList.get(position).getPublishedAt();
        int idx = date.indexOf(".");
        date = date.substring(0, idx).replace("T", " ");
        holder.mTvTechDate.setText(ZDate.formatDateTime(date, true));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    CardView cv = (CardView) view.findViewById(R.id.card);
                    mOnItemClickListener.onItemClick(holder.getAdapterPosition(), cv);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_tech_title)
        TextView mTvTechTitle;
        @BindView(R.id.tv_tech_author)
        TextView mTvTechAuthor;
        @BindView(R.id.tv_tech_date)
        TextView mTvTechDate;
        @BindView(R.id.card)
        CardView mCard;
        //        @BindView(R.id.icon_item_tech)
        //        ImageView mIconItemTech;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //item回调
    public interface OnItemClickListener {
        void onItemClick(int position, View view);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    private OnItemClickListener mOnItemClickListener;
}
